package com.zy.springboot.com;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.text.DecimalFormat;

/**
 * Created by y on 2019/1/29.
 */
public class Base64Util {
    //图片转化成base64字符串
    public static String GetImageStr(String path) {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        String imgFile = path;//待处理的图片 "D://tupian//a.jpg"
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);//返回Base64编码过的字节数组字符串
    }

    //base64字符串转化成图片
    public static JSONObject GenerateImage(String imgStr, String fileName) {  //对字节数组字符串进行Base64解码并生成图片
        JSONObject object=new JSONObject();
        if (imgStr == null){
            object.put("result","no");
            return object;
        } //图像数据为空

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {//调整异常数据
                    b[i] += 256;
                }
            }
            String fileSize=getNetFileSizeDescription(b.length);
            String extension=getExtName(fileName);
            String fileUrl="../images/tx/"+fileName;
            object.put("result","yes");
            JSONObject attachment=new JSONObject();
            attachment.put("fileSize",fileSize);
            attachment.put("extension",extension);
            attachment.put("fileUrl",fileUrl);
            attachment.put("fileName",fileName);
            object.put("attachment",attachment);
            //生成jpeg图片../images/index.jpg
            String imgFilePath = "E:/webstorm/pages/images/tx/"+fileName;//新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return object;
        } catch (Exception e) {
            object.put("result","no");
            return object;
        }
    }
    public static String getNetFileSizeDescription(long size) {
        StringBuffer bytes = new StringBuffer();
        DecimalFormat format = new DecimalFormat("###.0");
        if (size >= 1024 * 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0 * 1024.0));
            bytes.append(format.format(i)).append("GB");
        }
        else if (size >= 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0));
            bytes.append(format.format(i)).append("MB");
        }
        else if (size >= 1024) {
            double i = (size / (1024.0));
            bytes.append(format.format(i)).append("KB");
        }
        else if (size < 1024) {
            if (size <= 0) {
                bytes.append("0B");
            }
            else {
                bytes.append((int) size).append("B");
            }
        }
        return bytes.toString();
    }
    public static String getExtName(String filename) {
        if (StringUtils.isBlank(filename)) {
            return null;
        }
        int i = filename.indexOf(".");
        if (i >= 0) {
            return filename.substring(i + 1).trim().toLowerCase();
        }
        return null;
    }
}
